<?php
use \Model\Target;
use \Model\Account;
class Controller_Target extends Controller_Weibo{
	function action_portal() {
		$page_config = Config::get('pagination.target', array());
		$page_config['total_items'] = Target::get_cnt(Session::get('user_id', 0));
		$page = Uri::segment(2, 1);
		Config::set('pagination', $page_config);
		Pagination::$current_page = $page;
		$target = Target::get_account(Session::get('user_id', 0), Pagination::$per_page, Pagination::$per_page * (Pagination::$current_page - 1));
		$page_links = Pagination:: create_links();
		$prev_link = Pagination::prev_link('prev');
		$next_link = Pagination::next_link('next');
		$this->data['content'] = View::forge('target/content', array('target' => $target, 'page_links' => $page_links, 'next_link' => $next_link, 'prev_link' => $prev_link));
		return Response::forge(View::forge('layout', $this->data));
	}

	function action_get_info() {
		$screen_name = Input::get('screen_name', '');
		if ($screen_name == '') {
			return self::msg(array('msg' => 'fail', 'data' => array()));
		} else {
			$access_token = Account::get_access_token();
			$c = new SaeTClientV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', '') , $access_token);
			$user_message = $c->show_user_by_name( $screen_name);//根据昵称获取用户等基本信息
			if (!isset($user_message['name']) or !$user_message['name']) {
				return self::msg(array('msg' => 'fail', 'data' => array()));
			}
			$data['name'] = $user_message['name'];
			$data['screen_name'] = $user_message['screen_name'];
			$data['idstr'] = $user_message['idstr'];
			$data['profile_image_url'] = $user_message['profile_image_url'];
			$data['url'] = $user_message['url'];
			$data['location'] = $user_message['location'];
			$data['description'] = $user_message['description'];
			$data['followers_count'] = $user_message['followers_count'];
			$data['friends_count'] = $user_message['friends_count'];
			$data['statuses_count'] = $user_message['statuses_count'];
			$data['user_id'] = Session::get('user_id', 0);
			$data['updated_at'] = date('Y-m-d H:i:s');
			Target::add($data);
			return self::msg(array('msg' => 'succ', 'data' => $data));
		}
	}
}
